package com.yms.controller;



import com.mysql.jdbc.TimeUtil;
import com.yms.pojo.*;
import com.yms.service.IHostService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-03-20
 */
@Controller
@RequestMapping("/host")
public class HostController {
    //声明业务层属性
    @Autowired
    private IHostService hostService;
    //声明单元方法:分页加载主持人信息
    /**
     * dataGrid的数据格式：
     *   要求响应一个json数据，并且有total和rows两个属性
     *   total表示符合要求的总的数据量
     *   rows为当前请求页的数据的list集合
     *   注意:
     *      创建实体类，实体类中有两个属性，属性名必须为total，rows
     *
     * 单元方法的形参:
     *      请求的页码： page
     *      每页显示的数量: rows
     *      筛选条件:HostCondition
     */
    @ResponseBody
    @RequestMapping("hostInfo")
    public PageResult<Host> hostInfo(Integer page, Integer rows, HostCondition hostCondition){
        //处理请求
            PageResult<Host> pageResult=hostService.selHostInfoService(page,rows,hostCondition);
        //响应结果
            return pageResult;
    }

    //batchOperator实现
    @RequestMapping("batchOperator")
    @ResponseBody
    public Result batchOperator(HostCondition hostCondition, HttpServletResponse resp) {
        List<Host> hostList  = hostService.selHostMore(hostCondition);
        System.out.println(hostList);
        if (hostList != null) {
            this.createExcel(hostList, resp);
            return new Result(true, "导出成功");
        } else {
            return new Result(false, "导出失败");
        }
    }




    //声明单元方法:新增主持人信息
    @ResponseBody
    @RequestMapping("hostAdd")
    public Result hostAdd(Host host){
        //处理请求
            //设置主持人默认数据
            host.setStarttime(LocalDateTime.now());
            host.setStatus("1");
            host.setStrong("20");
            host.setOrdernumber(0);
            //新增
            boolean insert = host.insert();
        //响应结果
            return  new Result(insert,insert?"增加成功":"增加失败");
    }

    //声明单元方法:修改主持人账号状态
    @ResponseBody
    @RequestMapping("hostAccountUp")
    public Result hostAccountUp(String hids,String statuss){
        //处理请求
            //获取要修改的主持人ID的数组
            String[] hidsStr=hids.split(",");
            //获取主持人当前账号状态
            String[] statussStr=statuss.split(",");
            //修改
            for(int i=0;i<hidsStr.length;i++){
                //修改主持人账号状态
                Host host=new Host();
                host.setHid(Integer.valueOf(hidsStr[i]));
                host.setStatus("1".equals(statussStr[i])?"0":"1");
                //更新
                host.updateById();
            }
        //响应结果
        return  new Result(true,"");
    }
    //声明单元方法:修改主持人权重
    @ResponseBody
    @RequestMapping("hostStrongUp")
    public Result hostStrongUp(Host host){
        //处理请求
         boolean b = host.updateById();
        //响应结果
        return  new Result(b,"");
    }


    //文件导出功能
    private  void createExcel(List<Host> list, HttpServletResponse resp) {

        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("主持人信息表");

        CellRangeAddress region = new CellRangeAddress(0, // first row
                0, // last row
                0, // first column
                7 // last column
        );
        sheet.addMergedRegion(region);

        HSSFRow hssfRow = sheet.createRow(0);
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("主持人列表");

        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        headCell.setCellStyle(cellStyle);

        // 添加表头行
        hssfRow = sheet.createRow(1);
        // 添加表头内容
        headCell = hssfRow.createCell(0);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("手机号");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("开通时间");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("价格");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(4);
        headCell.setCellValue("订单量");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(5);
        headCell.setCellValue("折扣");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(6);
        headCell.setCellValue("星推荐");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(7);
        headCell.setCellValue("账号状态");
        headCell.setCellStyle(cellStyle);


        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 2);
            Host host = list.get(i);
            HostPower hostPower = host.getHostPower();

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(host.getHname());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(host.getHphone());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            LocalDateTime starttime = host.getStarttime();
            String time =starttime.getYear()+"-"+starttime.getMonth()+
                    "-"+starttime.getDayOfMonth()+
                    "-"+starttime.getHour()+":"+starttime.getMinute()
                    +":"+starttime.getSecond();
            cell.setCellValue(time);
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(3);
            cell.setCellValue(hostPower.getHpprice());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(4);
            cell.setCellValue(host.getOrdernumber());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(5);
            cell.setCellValue(hostPower.getHpdiscount());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(6);
            cell.setCellValue(hostPower.getHpstar());

            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(7);
            cell.setCellValue(host.getStatus());
            cell.setCellStyle(cellStyle);


        }

        // 保存Excel文件
        try {
            /*设置响应内容为excel文件*/
            resp.setContentType("application/vnd.ms-excel");
            /*告知浏览器响应的数据以文件形式接收*/
            resp.setHeader("Content-Disposition","attachment;filename=zhc"+formater.format(new Date())+".xls");
            OutputStream outputStream = resp.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

